package com.survey.project.research.controller;

import java.math.BigDecimal;
import java.util.*;

import com.survey.common.utils.ServletUtils;
import com.survey.framework.security.LoginUser;
import com.survey.framework.security.service.TokenService;
import com.survey.project.research.domain.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.survey.framework.aspectj.lang.annotation.Log;
import com.survey.framework.aspectj.lang.enums.BusinessType;
import com.survey.project.research.domain.Questionnaire;
import com.survey.project.research.service.IQuestionnaireService;
import com.survey.framework.web.controller.BaseController;
import com.survey.framework.web.domain.AjaxResult;
import com.survey.common.utils.poi.ExcelUtil;
import com.survey.framework.web.page.TableDataInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 调研问卷Controller
 *
 * @author survey
 * @date 2021-05-20
 */
@Api("调研问卷")
@RestController
@RequestMapping("/research/questionnaire")
public class QuestionnaireController extends BaseController {
    @Autowired
    private IQuestionnaireService questionnaireService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询调研问卷列表
     */
    @PreAuthorize("@ss.hasPermi('research:questionnaire:list')")
    @GetMapping("/list")
    public TableDataInfo list(Questionnaire questionnaire) {
        startPage();
        List<Questionnaire> list = questionnaireService.selectQuestionnaireList(questionnaire);
        return getDataTable(list);
    }

    /**
     * 导出调研问卷列表
     */
    @PreAuthorize("@ss.hasPermi('research:questionnaire:export')")
    @Log(title = "调研问卷", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Questionnaire questionnaire) {
        List<Questionnaire> list = questionnaireService.selectQuestionnaireList(questionnaire);
        ExcelUtil<Questionnaire> util = new ExcelUtil<Questionnaire>(Questionnaire.class);
        return util.exportExcel(list, "questionnaire");
    }

    /**
     * 获取调研问卷详细信息
     */
    @PreAuthorize("@ss.hasPermi('research:questionnaire:query')")
    @GetMapping(value = "/{questionnaireId}")
    public AjaxResult getInfo(@PathVariable("questionnaireId") Long questionnaireId) {
        return AjaxResult.success(questionnaireService.selectQuestionnaireById(questionnaireId));
    }

    /**
     * 新增调研问卷
     */
    @PreAuthorize("@ss.hasPermi('research:questionnaire:add')")
    @Log(title = "调研问卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Questionnaire questionnaire) {
        return toAjax(questionnaireService.insertQuestionnaire(questionnaire));
    }

    /**
     * 修改调研问卷
     */
    @PreAuthorize("@ss.hasPermi('research:questionnaire:edit')")
    @Log(title = "调研问卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Questionnaire questionnaire) {
        return toAjax(questionnaireService.updateQuestionnaire(questionnaire));
    }

    /**
     * 调研问卷状态修改
     */
    @Log(title = "调研问卷", businessType = BusinessType.UPDATE)
    @PostMapping("/status")
    public AjaxResult changeQuestionnaireStatus(@RequestBody Questionnaire questionnaire) {
        return toAjax(questionnaireService.changeQuestionnaireStatus(questionnaire));
    }

    /**
     * 删除调研问卷
     */
    @PreAuthorize("@ss.hasPermi('research:questionnaire:remove')")
    @Log(title = "调研问卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionnaireIds}")
    public AjaxResult remove(@PathVariable Long[] questionnaireIds) {
        return toAjax(questionnaireService.deleteQuestionnaireByIds(questionnaireIds));
    }

    /**
     * 查询调研问卷问题列表
     */
    @ApiOperation("查询调研问卷问题列表")
    @PreAuthorize("@ss.hasPermi('research:questionnaire:question:list')")
    @GetMapping("/question/list/{questionnaireId}")
    public TableDataInfo researchQuestionnaireQuestionList(@PathVariable("questionnaireId") Long questionnaireId) {
        startPage();
        List<QuestionnaireQuestionListOutVo> list = questionnaireService.researchQuestionnaireQuestionList(questionnaireId);
        return getDataTable(list);
    }

    /**
     * 查询调研问卷客户列表
     */
    @ApiOperation("查询调研问卷客户列表")
    @PreAuthorize("@ss.hasPermi('research:questionnaire:consumer:list')")
    @GetMapping("/consumer/list")
    public TableDataInfo researchQuestionnaireConsumerList(Questionnaire questionnaire) {
        startPage();
        List<ConsumerOutVo> list = questionnaireService.researchQuestionnaireConsumerList(questionnaire);
        return getDataTable(list);
    }

    /**
     * 导入题库列表
     */
    @ApiOperation("导入题库列表")
    @PreAuthorize("@ss.hasPermi('research:questionnaire:question:import')")
    @PostMapping("/question/import")
    public AjaxResult researchQuestionnaireQuestionImport(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<QuestionnaireQuestionExcelVo> util = new ExcelUtil<>(QuestionnaireQuestionExcelVo.class);
        List<QuestionnaireQuestionExcelVo> questionList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = questionnaireService.importQuestion(questionList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载题库导入模板
     */
    @GetMapping("/question/importTemplate")
    public AjaxResult questionImportTemplate() {
        ExcelUtil<QuestionnaireQuestionExcelVo> util = new ExcelUtil<QuestionnaireQuestionExcelVo>(QuestionnaireQuestionExcelVo.class);
        return util.importTemplateExcel("问卷问题列表数据");
    }

    /**
     * 导入客户列表
     */
    @ApiOperation("导入客户列表")
    @PreAuthorize("@ss.hasPermi('research:questionnaire:consumer:import')")
    @PostMapping("/consumer/import")
    public AjaxResult researchQuestionnaireConsumerImport(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ConsumerExcelVo> util = new ExcelUtil<>(ConsumerExcelVo.class);
        List<ConsumerExcelVo> consumerList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = questionnaireService.importConsumer(consumerList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载题库导入模板
     */
    @GetMapping("/consumer/importTemplate")
    public AjaxResult consumerImportTemplate() {
        ExcelUtil<ConsumerExcelVo> util = new ExcelUtil<ConsumerExcelVo>(ConsumerExcelVo.class);
        return util.importTemplateExcel("问卷客户列表数据");
    }

    /**
     * 根据客户id获取对应题目信息
     */
    @GetMapping("/consumer/questions/{consumerId}")
    public TableDataInfo consumerQuestions(@PathVariable("consumerId") Long consumerId) {
        List<QuestionnaireQuestionListOutVo> list = questionnaireService.consumerQuestions(consumerId);
        return getDataTable(list);
    }

    /**
     * 客户提交问卷
     */
    @PostMapping("/consumer/commit")
    public AjaxResult consumerCommit(@RequestBody ConsumerAnswerInVo inVo) {
        questionnaireService.consumerCommit(inVo);
        return AjaxResult.success();
    }

    /**
     * 通过选项id获取选项详情
     */
    @GetMapping("/question/choice/{choiceId}")
    public AjaxResult getChoiceByChoiceId(@PathVariable("choiceId") Long choiceId) {
        QuestionChoiceListOutVo outVo = questionnaireService.getChoiceByChoiceId(choiceId);
        return AjaxResult.success(outVo);
    }

    /**
     * 通过选项id获取多选选项详情
     */
    @GetMapping("/question/choice/check/{choiceId}")
    public AjaxResult getChoiceCheckByChoiceId(@PathVariable("choiceId") Long choiceId) {
        QuestionChoiceCheckListOutVo outVo = questionnaireService.getChoiceCheckByChoiceId(choiceId);
        return AjaxResult.success(outVo);
    }

    /**
     * 动态数据导出测试
     */
    @GetMapping("/test/{questionnaireId}")
    public AjaxResult test(@PathVariable("questionnaireId") Long questionnaireId) {
        // 封装需要导出的所有动态数据
        ArrayList<DynamicDataExcelVo> list = new ArrayList<>();
        // 表头数据
        List<List<String>> headLists = questionnaireService.getQuestionListByQuestionnaireId(questionnaireId);
        // 表数据
        List<List<Object>> dataLists = questionnaireService.getConsumerAnswerByQuestionnaireId(questionnaireId);
        DynamicDataExcelVo vo = new DynamicDataExcelVo();
        vo.setHeadList(headLists);
        vo.setDataList(dataLists);
        list.add(vo);
        ExcelUtil<Object> util = new ExcelUtil<>(Object.class);
        return  util.exportDynamicEasyExcel(list,"测试");


    }

}
